<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\basics\model\Port as PortModel;
use catcher\CatchAuth;
use catchAdmin\basics\request\CreateRequest;
class Port extends CatchController
{
    
    protected $portModel;
    
    /**
     *
     * @time 2022/02/28 14:26
     * @param PortModel $portModel
     * @return mixed
     */
    public function __construct(PortModel $portModel)
    {
        $this->portModel = $portModel;
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->portModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/28 14:25
     * @param CreateRequest $request
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,CreateRequest $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->portModel->storeBy($params),"新建成功");
    }
    
    /**
     *
     * @time 2022/02/28 14:25
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->portModel->updateBy($params['id'], $params),"修改成功");
    }
    
    /**
     * 删除
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->portModel->deleteBy($id),'删除成功');
    }

    /**
     * @time 2022/02/28 14:25
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function  getPortNum(Request $request){
        $data = $this->portModel->column('port_num');
        return CatchResponse::success($data, '查询成功');
    }
}